﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Domain.CarKpi;
using LX.Web.Excel;

namespace S4Store.Domain.Report
{
    public class CarModelSaleReport : CarSaleCommonReport
    {
        #region 数据导出
        [Export("车型", MergeVertical = true)]
        public String CarBrandName
        {
            get
            {
                return CarModel.Brand.Name;
            }
        }


        [Export("车款")]
        public String CarModelName
        {
            get
            {
                return CarModel.Name;
            }
        }

        #endregion
        

        private List<CarKpiDayData> _carKpiDayDatas = new List<CarKpiDayData>();
        public List<CarKpiDayData> CarKpiDayDatas {
            get {
                if (!showlastday || !_carKpiDayDatas.Any())
                {
                    return _carKpiDayDatas;
                }
                else
                {
                    List<CarKpiDayData> result = new List<CarKpiDayData>();
                    result.Add(_carKpiDayDatas.OrderByDescending(t => t.Date).First());
                    return result;
                }
            }
            set { _carKpiDayDatas = value; }
        }
        public CarModelMonthConfig CarModelMonthConfig { get; set; }

        

        public CarModel CarModel { get; set; }
        public CarSaleReport StoreReport { get; set; }

        public CarModelSaleReport()
        {
        }

        /// <summary>
        /// 本店交车
        /// </summary>
        [Export("本店交车")]
        public override int BDYJ
        {
            get
            {
                return CarKpiDayDatas.Sum(t => t.BDYJ);
            }
        }

        /// <summary>
        /// 二网交车
        /// </summary>
        [Export("二网交车")]
        public override int EWYJ
        {
            get
            {
                return CarKpiDayDatas.Sum(t => t.EWYJ);
            }
        }

        /// <summary>
        /// 本店已订
        /// </summary>
        [Export("本店已订")]
        public override int BDYD
        {
            get
            {
                return CarKpiDayDatas.Sum(t => t.BDYD);
            }
        }

        /// <summary>
        /// 二网已订
        /// </summary>
        [Export("二网已订")]
        public override int EWYD
        {
            get
            {
                return CarKpiDayDatas.Sum(t => t.EWYD);
            }
        }


        /// <summary>
        /// 销售目标
        /// </summary>
        [Export("销售目标")]
        public override int SaleTarget
        {
            get
            {
                return CarModelMonthConfig.SaleTarget;
            }
        }

        /// <summary>
        /// 订单进度
        /// </summary>
        [Export("订单进度", Format = "{0}%", BackBround = ExcelCellBg.Calculate)]
        public override decimal OrderRate
        {
            get
            {
                if (OrderTarget == 0) return RateOfTime;
                return (decimal)(OrderCount + CarModelMonthConfig.LastMonthNotGive) * 100 / (decimal)OrderTarget;
            }
        }

        private  CarKpiDayData LastDateData
        {
            get
            {
                if (CarKpiDayDatas == null || !CarKpiDayDatas.Any()) return new CarKpiDayData();
                var data= CarKpiDayDatas.OrderByDescending(t => t.Date).First();
                return data;
            }
        }

        /// <summary>
        /// A类库存
        /// </summary>
        [Export("A类库存")]
        public override int AKC
        {
            get
            {
                return LastDateData.AKC;
            }
        }

        /// <summary>
        /// B类库存
        /// </summary>
        [Export("B类库存")]
        public override int BKC
        {
            get
            {
                return LastDateData.BKC;
            }
        }

        /// <summary>
        /// C类库存
        /// </summary>
        [Export("C类库存")]
        public override int CKC
        {
            get
            {
                return LastDateData.CKC;
            }
        }
        /// <summary>
        /// 在途
        /// </summary>
        [Export("在途")]
        public override int ZT
        {
            get
            {
                return LastDateData.ZT;
            }
        }
        /// <summary>
        /// C类库龄
        /// </summary>
        [Export("C类库龄")]
        public override int CKL
        {
            get
            {
                return LastDateData.CKL;
            }
        }
        /// <summary>
        /// 预提
        /// </summary>
        [Export("预提")]
        public override int YT
        {
            get
            {
                return CarModelMonthConfig.JHTC;
            }
        }

        /// <summary>
        /// 上月库存
        /// </summary>
        [Export("上月库存")]
        public override int LastMonthInventory
        {
            get
            {
                return CarModelMonthConfig.LastMonthInventory;
            }
        }


        /// <summary>
        /// 上月交车
        /// </summary>
        [Export("上月交车")]
        public override int LastMonthGiveCar
        {
            get
            {
                return CarModelMonthConfig.LastMonthGive;
            }
        }



        /// <summary>
        /// 投保数量
        /// </summary>
        [Export("累计投保")]
        public override decimal TBSL
        {
            get
            {
                return this.CarKpiDayDatas.Sum(t => t.TB);
            }
        }


        /// <summary>
        /// 置换数量
        /// </summary>
        [Export("累计置换")]
        public override decimal ZHSL
        {
            get
            {
                return this.CarKpiDayDatas.Sum(t => t.ZH);
            }
        }


        /// <summary>
        /// 按揭数量
        /// </summary>
        [Export("累计按揭")]
        public override decimal AJSL
        {
            get
            {
                return this.CarKpiDayDatas.Sum(t => t.AJ);
            }
        }


        /// <summary>
        /// 延保数量
        /// </summary>
        [Export("累计延保")]
        public override decimal YBSL
        {
            get
            {
                return this.CarKpiDayDatas.Sum(t => t.YB);
            }
        }

        /// <summary>
        /// 上旬套餐版本交车量
        /// </summary>
        [Export("上旬套餐版本交车量")]
        public override int LstTenVersionCount
        {
            get { return this.CarKpiDayDatas.Sum(t => t.LstTenVersion); }
        }

        /// <summary>
        /// 中旬套餐版本交车量
        /// </summary>
        [Export("中旬套餐版本交车量")]
        public override int MidTenVersionCount
        {
            get { return this.CarKpiDayDatas.Sum(t => t.MidTenVersion); }
        }

        /// <summary>
        /// 下旬套餐版本交车量
        /// </summary>
        [Export("下旬套餐版本交车量")]
        public override int LastTenVersionCount
        {
            get { return this.CarKpiDayDatas.Sum(t => t.LastTenVersion); }
        }

        [Export("是否上旬套餐版本")]
        public string IsLstVersion
        {
            get { return CarModelMonthConfig.IsLstTenVersion ? "是" : "否"; }
        }

        [Export("是否中旬套餐版本")]
        public string IsMidVersion
        {
            get { return CarModelMonthConfig.IsMidTenVersion ? "是" : "否"; }
        }

        [Export("是否下旬套餐版本")]
        public string IsLastVersion
        {
            get { return CarModelMonthConfig.IsLastTenVersion ? "是" : "否"; }
        }

        [Export("上旬版本计数")]
        public int LstVersionHolder
        {
            get { return CarModelMonthConfig.IsLstTenVersion ? 1 : 0; }
        }

        [Export("中旬版本计数")]
        public int MidVersionHolder
        {
            get { return CarModelMonthConfig.IsMidTenVersion ? 1 : 0; }
        }

        [Export("下旬版本计数")]
        public int LastVersionHolder
        {
            get { return CarModelMonthConfig.IsLastTenVersion ? 1 : 0; }
        }


        [Export("累计版本计数")]
        public int VersionCount
        {
            get { return LstVersionHolder + MidVersionHolder + LastVersionHolder; }
        }
        /// <summary>
        /// 装潢车交车量
        /// </summary>
        [Export("装潢车交车量")]
        public override int DecorateCount
        {
            get { return CarKpiDayDatas.Sum(t => t.NotPackageDecorateCount); }
        }

        /// <summary>
        /// 装潢总额
        /// </summary>
        [Export("装潢总额")]
        public override decimal DecorateAmout
        {
            get { return CarKpiDayDatas.Sum(t => t.NotPackageDecorateValue); }
        }

        /// <summary>
        /// 单车装潢总额
        /// </summary>
        [Export("单车装潢总额",Precision=3)]
        public override decimal DecorateAmoutOfSingCar
        {
            get
            {
                return CarKpiDayDatas.Where(t => t.NotPackageDecorateCount>0).AverageOrDefault(t=>t.NotPackageDecorateValue);
            }
        }


        /// <summary>
        /// 套餐车型数
        /// </summary>
        public override int PackageCarModelCount
        {
            get
            {
                return CarModelMonthConfig.PackageCarModelCount;
            }
        }


        /// <summary>
        /// 套餐车设计率
        /// </summary>
        [Export("套餐车设计率", Format = "{0}%")]
        public override decimal PackageCarSJLV
        {
            get
            {
                return (PackageCarModelCount/3) * 100;
            }
        }


        /// <summary>
        /// 套餐车更换率
        /// </summary>
        [Export("套餐车更换率", Format = "{0}%")]
        public override decimal PackageVersionChangeLv
        {
            get
            {
                return ((decimal)(PackageCarModelCount*100))/(decimal)3;
            }
        }

        /// <summary>
        /// H有望客户
        /// </summary>
        [Export("H级有望客户")]
        public override int HYWKH
        {
            get { return CarKpiDayDatas.Sum(t => t.HYWKH); }
        }

        /// <summary>
        /// A有望客户
        /// </summary>
        [Export("A级有望客户")]
        public override int AYWKH
        {
            get { return CarKpiDayDatas.Sum(t => t.AYWKH); }
        }
        /// <summary>
        /// B有望客户
        /// </summary>
        [Export("B级有望客户")]
        public override int BYWKH
        {
            get { return CarKpiDayDatas.Sum(t => t.BYWKH); }
        }
        /// <summary>
        /// H登记客户
        /// </summary>
        [Export("H级登记客户")]
        public override int HDJKH
        {
            get { return CarKpiDayDatas.Sum(t => t.HDJKH); }
        }

        /// <summary>
        /// A登记客户
        /// </summary>
        [Export("A级登记客户")]
        public override int ADJKH
        {
            get { return CarKpiDayDatas.Sum(t => t.ADJKH); }
        }
        /// <summary>
        /// B登记客户
        /// </summary>
        [Export("B级登记客户")]
        public override int BDJKH
        {
            get { return CarKpiDayDatas.Sum(t => t.BDJKH); }
        }
    }
}
